﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ParsecParser;
using System.Xml.Linq;
using System.IO;
using System.Deployment.Application;

namespace ParsecClient.Classes
{
    [Serializable]
    public class ArchivedRaidFight
    {
        public static string RaidDataFileNamePrefix = "raid_";
        public int FightIndex { get; set; }
        public DateTime FightStart { get; set; }
        public List<ArchivedRaidFightMember> FightMembers { get; set; }
        public ArchivedRaidFight()
        {
            FightMembers = new List<ArchivedRaidFightMember>();
        }
        
        private static System.Xml.Serialization.XmlSerializer RaidDataFileSerializer = new System.Xml.Serialization.XmlSerializer(typeof(ArchivedRaidFile));
        private static string GetFullPath(string logFileName)
        {
            
            string folder;
            if (ApplicationDeployment.IsNetworkDeployed)
            {
                folder = ApplicationDeployment.CurrentDeployment.DataDirectory;
            }
            else
            {
                folder = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Parsec");
                if (!Directory.Exists(folder))
                {
                    Directory.CreateDirectory(folder);
                }
            }
            string fileName = Path.ChangeExtension(RaidDataFileNamePrefix + logFileName, "xml");
            return Path.Combine(folder, fileName);
        }
        public static void ArchiveRaidData(string logFileName, ArchivedRaidFight data)
        {
            if (string.IsNullOrEmpty(logFileName))
                throw new ArgumentException("parameter logFileName is null or empty", "logFileName");
            string fullPath = GetFullPath(logFileName);
            //If file doesnt exist
            if (!File.Exists(fullPath))
            {
                ArchivedRaidFile rdf = new ArchivedRaidFile() { RaidFights = new List<ArchivedRaidFight>() {data} };
                SerializeDataToDisk(rdf, fullPath);
            }
            else
            {
                //load file
                ArchivedRaidFile rdf = DeserializeDataFromDisk(fullPath, false);

                //if fight index exists update data
                //rdf.RaidFights.RemoveAll(x => x.FightStart == data.FightStart);
                if (rdf.RaidFights.FirstOrDefault(x => x.FightStart == data.FightStart) == null)
                {
                    rdf.RaidFights.Add(data);
                }

                //rewrite file
                SerializeDataToDisk(rdf, fullPath);
            }
        }
        public static ArchivedRaidFile RetrieveRaidData(string logFileName)
        {
            if (string.IsNullOrEmpty(logFileName))
                throw new ArgumentException("parameter logFileName is null or empty", "logFileName");
            string fullPath = GetFullPath(logFileName);
            if (File.Exists(fullPath))
                return DeserializeDataFromDisk(fullPath, false);
            else
                return null;
        }
        private static void SerializeDataToDisk(ArchivedRaidFile rdf, string fullPath)
        {            
            using (System.IO.StreamWriter writer = new System.IO.StreamWriter(fullPath))
            {
                RaidDataFileSerializer.Serialize(writer, rdf);
                //writer.Close();
            }
        }
        private static ArchivedRaidFile DeserializeDataFromDisk(string fullPath, bool suppressFailureException)
        {
            using (System.IO.StreamReader reader = new System.IO.StreamReader(fullPath))
            {
                try
                {
                    return (ArchivedRaidFile)RaidDataFileSerializer.Deserialize(reader);
                }
                catch
                {
                    if (suppressFailureException)
                        return null;
                    else
                        throw;
                }
            }
        }
    }
}
